<script lang="ts">
import { ref } from 'vue'
import { Phone, Message, MapLocation, User, SwitchButton } from '@element-plus/icons-vue'
import { Action, ElMessage, ElMessageBox } from 'element-plus';

import { submitFeedback } from '@/api/call.js';
import { useTokenStore } from '@/stores/token';
import router from '@/router';

const call = ref(null);

const getCall = async () => {
    let result = await submitFeedback();
    call.value = result.data;
}
getCall();

const handleCommand = (command) => {
    if (command === 'logout') {
        //退出登录
        ElMessageBox.confirm(
            '你确认退出登录吗？',
            '温馨提示',
            {
                confirmButtonText: '确认',
                cancelButtonText: '取消',
                type: 'warning',
            }
        )
            .then(async () => {
                try {
                    // 清空pinia中的token和个人信息
                    const tokenStore = useTokenStore();
                    tokenStore.removeToken();

                    // 跳转到登录页
                    router.push('/login');

                    // 更新 localStorage
                    localStorage.setItem('isLoggedIn', 'false');
                } catch (error) {
                    console.error("发生错误：", error);
                    // 可以选择是否在这里显示错误消息或其他处理
                }
            })
            .catch(() => {
                // 用户点击了取消
                ElMessage({
                    type: 'info',
                    message: '取消退出',
                });
            });
    }
}

export default {
    setup() {
        const rules = ref({
            feedback: [
                { required: true, message: '请简要描述您要反馈的意见或建议内容，最多可输入500字', trigger: 'blur' },
                { min: 1, max: 500, message: '最多只能输入500字', trigger: 'blur' }
            ],
        })
        const feedbackContent = ref('')
        const loading = ref(false)

        const submitFeedbackForm = async () => {
            if (!feedbackContent.value.trim()) {
                ElMessage.warning('请输入反馈内容')
                return
            }

            loading.value = true
            try {
                const response = await submitFeedback({
                    content: feedbackContent.value
                })
                
                if (response.code === 0) {
                    ElMessage.success('感谢您的反馈！')
                    feedbackContent.value = '' // 清空输入
                } else {
                    ElMessage.error(response.msg || '提交失败，请重试')
                }
            } catch (error) {
                console.error('提交反馈失败:', error)
                ElMessage.error('提交失败，请稍后重试')
            } finally {
                loading.value = false
            }
        }

        return {
            rules,
            submitFeedbackForm,
            handleCommand,
            Phone,
            Message,
            MapLocation,
            User,
            SwitchButton,
            activeIndex: '',
            handleSelect: '',
            searchQuery: '',
            isLoggedIn: false,
            isDark: false,
            userAvatar: 'https://wpimg.wallstcn.com/f778738c-e4f8-4870-b634-56703b4acafe.gif',
            feedbackContent,
            loading,
        }
    },
    components: {
        Phone,
        Message,
        MapLocation,
        User,
        SwitchButton,
    },
    methods: {
        setDefaultLogo(event) {
            event.target.src = 'images/default-logo.jpg';
        },
        search() {
            // 处理搜索逻辑
            console.log(`搜索内容: ${this.searchQuery}`);
        },
        filter(option) {
            // 处理过滤逻辑
            console.log(`过滤选项: ${option}`);
        },
        navigateTologin(page) {
            this.$router.push(`/${page}`); // 跳转到登录页面
        },
        navigateToregister(page) {
            this.$router.push(`/${page}`); // 跳转到登录页面
        },
        navigateTo(link) {
            window.location.href = link;
        },
    }
}
</script>

<template>
    <el-menu :default-active="activeIndex" class="el-menu-demo" mode="horizontal" :ellipsis="false"
        @select="handleSelect" style="height:75px;box-shadow: 0 2px 4px rgba(0,0,0,0.1);">
        <el-menu-item index="0">
            <img style="width: 100px;height: 70px;margin-left: 53px;margin-right: 10px;" src="@/assets/logo.jpg"
                alt="CodeCash" />
        </el-menu-item>
        <el-menu-item index="1" style="transform: translate(-76%,-1px);">
            <el-input v-model="searchQuery" style="width: 300px;height:40px;" placeholder="搜索您感兴趣的内容" clearable />
            <el-button type="primary" class="search-button" style="height: 40px;background-color:#007bff;color:white;">搜索</el-button>
        </el-menu-item>
        <el-router-menu index="2" style="transform: translateX(-2%);">
            <el-link href="/" style="color: black; font-size: 18px;padding: 20px;">首页</el-link>
            <el-link href="/programmer" style="color: black; font-size: 18px;padding: 20px;">程序员</el-link>
            <el-link href="/task" style="color: black; font-size: 18px;padding: 20px;">任务大厅</el-link>
            <el-link href="/community" style="color: black; font-size: 18px;padding: 20px;">社区</el-link>
            <el-link href="/user/call" style="color: black; font-size: 18px;padding: 20px;">联系我们</el-link>
        </el-router-menu>
        <el-buttons-item index="3" style="transform: translateY(25%);margin-right: 20px;">
            <el-dropdown placement="bottom-end">
                <el-avatar :src="userAvatar" size="medium" />
                <el-icon>
                    <CaretBottom />
                </el-icon>
                <template #dropdown>
                    <el-dropdown-menu>
                        <el-dropdown-item command="userInfo" :icon="User"
                            @click="navigateTo('/userInfo')">个人中心</el-dropdown-item>
                        <el-dropdown-item command="logout" :icon="SwitchButton"
                            @click="handleCommand('logout')">退出登录</el-dropdown-item>
                    </el-dropdown-menu>
                </template>
            </el-dropdown>
        </el-buttons-item>
    </el-menu>
    <el-main>
        <div class="call-container">
            <img src="@/assets/jszc.png">
            <h2>为什么选择我们</h2>
            <p>我们提供全方位技术支持，确保您接单无忧，至今已服务90家优秀企业</p>
        </div>
        <div class="call-section">
            <img src="@/assets/ptzc.jpg">
            <ul>
                <h3>“码上有钱”程序员接单平台支持:</h3>
                <li>快速响应：24小时在线，快速解决您的问题。</li>
                <li>操作指导：详尽教程助您轻松上手平台功能。</li>
                <li>技术解答：专业团队解答开发难题，助力项目顺利进行。</li>
                <li>系统稳定：持续监控，保障平台稳定运行</li>
                <li>安全保障：严格保护您的数据安全与隐私。</li>
            </ul>
        </div>
        <h2>选择“码上有钱”，让技术不再是障碍，让接单更加顺畅！</h2>
        <div class="contact-container">
            <h2>联系我们</h2>
            <p>如果有任何问题或建议请联系我们，我们24小时竭诚为您服务</p>
            <div class="contact-info">
                <div>
                    <el-icon :size="50" style="position: absolute;top: 30px;left: 10px;">
                        <Phone />
                    </el-icon>
                    <h3>联系电话：</h3>
                    <p>100-000-0000</p>
                </div>
                <div>
                    <el-icon :size="50" style="position: absolute;top: 30px;left: 10px;">
                        <Message />
                    </el-icon>
                    <h3>公司邮箱：</h3>
                    <p>1234567890@qq.com</p>
                </div>
                <div>
                    <el-icon :size="50" style="position: absolute;top: 30px;left: 10px;">
                        <MapLocation />
                    </el-icon>
                    <h3>公司地址：</h3>
                    <p>广东省广州市天河区中山大道西191号</p>
                </div>
            </div>
        </div>
        <div class="Feedback">
            <h2>意见与建议反馈</h2>
            <div class="feedback-form">
                <label for="feedback">反馈内容：</label>
                <el-input
                    v-model="feedbackContent"
                    type="textarea"
                    :rows="6"
                    placeholder="请输入您的意见或建议（最多500字）"
                    maxlength="500"
                    show-word-limit
                />
                <el-button 
                    type="primary"
                    @click="submitFeedbackForm"
                    :loading="loading"
                    style="margin-top: 20px;"
                >
                    {{ loading ? '提交中...' : '提交反馈' }}
                </el-button>
            </div>
        </div>
    </el-main>
    <el-footer>
        <div class="footer">
            <div>
                <a href="#">动态公告</a> |
                <a href="#">帮助</a> |
                <a href="#">关于我们</a> |
                <a href="#">合作伙伴</a> |
                <a href="#">意见反馈</a> |
                <text style="color: rgb(78, 77, 77);">@“码上有钱”程序员接单平台</text>
            </div>
            <div class="credits">
                粤ICP备20220901号-4 | 粤公网安备33011002011599号
            </div>
        </div>
    </el-footer>
</template>

<style>
.el-menu--horizontal>.el-menu-item:nth-child(1) {
    margin-right: auto;
}

.call-container {
    margin-top: 50px;
    display: flex;
    justify-content: center;
    align-items: center;
    flex-direction: column;
}

.call-container img {
    max-width: 1000px;
    height: 300px;
    margin: auto;
    /* 这会居中图片 */
    display: block;
    /* 使块元素适用于自动边距 */
}

.call-section {
    display: flex;
    justify-content: center;
    align-items: center;
    margin: auto;
}

.call-section img {
    max-width: 350px;
    height: 220px;
    margin-left: 280px;
}

.call-section h3 {
    font-size: 24px;
    text-align: left;
    margin-bottom: 10px;
}

.call-section ul {
    margin-right: 380px;
}

.call-section ul li {
    text-align: left;
    margin-bottom: 10px;
}

.call-section h2 {
    margin-left: 500px;
}

.contact-container {
    justify-content: center;
    align-items: center;
    text-align: center;
    margin-left: 350px;
    margin: auto;
    max-width: 860px;
    border: 1px solid #ddd;
    padding: 20px;
    background-color: #fff;
    box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
}

.contact-container h2 {
    margin-bottom: 10px;
    font-size: 28px;
}

.contact-container p {
    margin-bottom: 20px;
    color: #555;
}

.contact-info {
    display: flex;
    justify-content: space-around;
}

.contact-info div {
    background-color: white;
    box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
    border-radius: 8px;
    width: 260px;
    height: 120px;
    padding: 20px;
    box-sizing: border-box;
    position: relative;
    margin: 10px;
}


.contact-info div h3 {
    margin: 0 0;
    position: absolute;
    top: 30px;
    left: 70px;
    font-size: 18px;
}

.contact-info div p {
    text-align: left;
    font-size: 14px;
    text-decoration: none;
    position: absolute;
    top: 50px;
    left: 70px;
}

.Feedback {
    max-width: 800px;
    margin: 40px auto;
    padding: 20px;
    background: #fff;
    border-radius: 8px;
    box-shadow: 0 2px 12px rgba(0, 0, 0, 0.1);
}

.Feedback h2 {
    text-align: center;
    margin-bottom: 30px;
    color: #333;
}

.el-form {
    max-width: 600px;
    margin: 0 auto;
}

.el-form-item:last-child {
    margin-bottom: 0;
    text-align: center;
}

.el-button {
    min-width: 120px;
}

h2 {
    text-align: center;
    margin-bottom: 20px;
}

label {
    text-align: left;
    font-size: 16px;
    margin-bottom: 10px;
    display: block;
}


.footer {
    margin-top: 30px;
    padding: 10px 0;
    border-top: 1px solid #ddd;
    text-align: center;
}

.footer a {
    text-decoration: none;
    color: rgb(78, 77, 77);
    background: transparent;
    border: none;
    cursor: pointer;
}

.footer .credits {
    margin-top: 5px;
    font-size: 12px;
    color: gray;
}

.feedback-form {
    max-width: 600px;
    margin: 0 auto;
    padding: 20px;
}

.el-button {
    width: 120px;
    margin: 0 auto;
    display: block;
}

.search-button {
    min-width: 80px !important;
    width: auto !important;
}
</style>